﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace YBBYunQi
{
    public sealed class SiteCache
    {
        public const int DayFactor = 17280;
        public const int HourFactor = 720;
        public const int MinuteFactor = 12;
        public const double SecondFactor = 0.2;
        private static Cache cache_0;
        private static int int_0;
        static SiteCache()
        {
            SiteCache.int_0 = 5;
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                SiteCache.cache_0 = current.Cache;
            }
            else
            {
                SiteCache.cache_0 = HttpRuntime.Cache;
            }
        }
        private SiteCache()
        {

        }
        public static void Clear()
        {
            IDictionaryEnumerator enumerator = SiteCache.cache_0.GetEnumerator();
            ArrayList arrayList = new ArrayList();
            while (enumerator.MoveNext())
            {
                arrayList.Add(enumerator.Key);
            }
            foreach (string key in arrayList)
            {
                SiteCache.cache_0.Remove(key);
            }
        }
        public static object Get(string key)
        {
            return SiteCache.cache_0[key];
        }
        public static void Insert(string key, object obj)
        {
            SiteCache.Insert(key, obj, null, 1);
        }
        public static void Insert(string key, object obj, int seconds)
        {
            SiteCache.Insert(key, obj, null, seconds);
        }
        public static void Insert(string key, object obj, CacheDependency dep)
        {
            SiteCache.Insert(key, obj, dep, 8640);
        }
        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            SiteCache.Insert(key, obj, null, seconds, priority);
        }
        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            SiteCache.Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                SiteCache.cache_0.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)(SiteCache.int_0 * seconds)), TimeSpan.Zero, priority, null);
            }
        }
        public static void Max(string key, object obj)
        {
            SiteCache.Max(key, obj, null);
        }
        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                SiteCache.cache_0.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }
        public static void Maxpage(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                SiteCache.cache_0.Insert(key, obj, dep, DateTime.Now.AddHours(12.0), TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }
        public static void MicroInsert(string key, object obj, int secondFactor)
        {
            if (obj != null)
            {
                SiteCache.cache_0.Insert(key, obj, null, DateTime.Now.AddSeconds((double)(SiteCache.int_0 * secondFactor)), TimeSpan.Zero);
            }
        }
        public static void Remove(string key)
        {
            SiteCache.cache_0.Remove(key);
        }
        public static void RemoveByPattern(string pattern)
        {
            IDictionaryEnumerator enumerator = SiteCache.cache_0.GetEnumerator();
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
            while (enumerator.MoveNext())
            {
                if (regex.IsMatch(enumerator.Key.ToString()))
                {
                    SiteCache.cache_0.Remove(enumerator.Key.ToString());
                }
            }
        }
        public static void ReSetFactor(int cacheFactor)
        {
            SiteCache.int_0 = cacheFactor;
        }
        public static int SecondFactorCalculate(int seconds)
        {
            return Convert.ToInt32(Math.Round((double)seconds * 0.2));
        }
    }
}
